<?php
require_once('include/inc_database.php');
require_once('include/inc_reccomment.php');

// Open DB connection.
$dbobj = new DatabaseObject();
$dbobj->OpenConnection();

header('Content-Type: application/xml');


if ($_GET['user_sub'] != "") {
	$username = $_GET['user_sub'];
	$sql = "SELECT cmt.*, " .
		"rec.recID, rec.username AS rec_username, rec.recText, rec.artist, usr.userIconPath " .
		"FROM mr_recComment cmt " .
		"INNER JOIN mr_useraccount usr ON cmt.username = usr.username " .
		"INNER JOIN mr_recommendation rec ON cmt.recID = rec.recID " .
		"WHERE (rec.username = " . ValToSQL_str($username) . " " .
			"OR rec.username IN (SELECT sub.subscribedToUsername FROM mr_subscription sub " .
				"WHERE sub.username = " . ValToSQL_str($username) . ")) " .
		"AND rec.disabled = 0 AND cmt.disabled = 0 " .
		"ORDER BY cmt.commentDate DESC";
	$feed_title = SiteConst('site_name') . " - Comments";
}

$cmt_ar = $dbobj->GetResultsAsNestedArray($sql, 100);


// Start output.
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
?>

<rss version='2.0'>
<channel>
<title><?=htmlspecialchars($feed_title)?></title>
<description></description>
<link>http://plurib.us/musicrecommend/user.php?user=<?=$username?></link>

<?$i = 0;
foreach ($cmt_ar as $cmt_r) {
	$i++;
	if ($cmt_r['disabled']) {?>
		<item>
			<title>Deleted comment</title>
			<guid><?=$cmt_r['recID']?>-<?=$cmt_r['commentNumber']?></guid>
			<!--<author><?=htmlspecialchars($cmt_r['username'])?></author>-->
			<pubDate><?=date("Y-m-d", strtotime($cmt_r["commentDate"]))?></pubDate>
		</item>
	<?}
	else {?>
		<item>
			<title><?=htmlspecialchars($cmt_r['recText'] . " (comment)")?></title>
			<link>http://plurib.us/musicrecommend/rec.php?recID=<?=$cmt_r['recID']?></link>
			<guid><?=$cmt_r['recID']?>-<?=$cmt_r['commentNumber']?></guid>
			<!--<author><?=htmlspecialchars($cmt_r['username'])?></author>-->
			<pubDate><?=date("Y-m-d", strtotime($cmt_r["commentDate"]))?></pubDate>
			<description>
				<?
				echo htmlspecialchars("<div>");
				if ($cmt_r['userIconPath'] != "") {
					echo htmlspecialchars("<a href='user.php?user=" . $cmt_r['username'] . "'><img src='image.php?usericon=" . $cmt_r['userIconPath'] . "&amp;size=16' align='ABSMIDDLE' style='border:solid 1px #aaa; padding:1px;'/></a> ");
				}
				echo htmlspecialchars("<b><i><a href='user.php?user=" . $cmt_r['username'] . "'>" . $cmt_r['username'] . "</a> said,</i></b> of <a href='rec.php?recID=" . $cmt_r['recID'] . "'>rec. by " . $cmt_r['rec_username'] . "</a>");
				echo htmlspecialchars("</div>")?>
				<?=htmlspecialchars(RecComment::CommentFormat($cmt_r['content']))?>
			</description>
		</item>
	<?}?>
<?}?>

</channel>
</rss>
